var xmlhttp;

function getXmlHttpObject()
{
	if (window.XMLHttpRequest)
	  {// code for IE7+, Firefox, Chrome, Opera, Safari
	  xmlhttp=new XMLHttpRequest();
	  }
	else
	  {// code for IE6, IE5
	  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	  }
	return xmlhttp;
}

function addAdvertising()
{
	getXmlHttpObject();
	var title = document.getElementById("title").value;
	var summary = document.getElementById("summary").value;
	var details = document.getElementById("details").value;
	var status = document.getElementById("status").value;
	var adUrl = document.getElementById("adUrl").value;
	var category = document.getElementById("category").value;
	
	details = details.replace("\t","");
	
	var url = "../controllers/addAdvertisingController.php";
	var params = "title=" + title + "&summary=" + summary + "&details=" + details + "&adurl=" + adUrl + "&status=" + status + "&category=" + category; 
	xmlhttp.open("POST",url,true);
	xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
	xmlhttp.onreadystatechange = addAdvertisingSuccess;
	xmlhttp.send(params);
}

function addAdvertisingSuccess()
{
	if (xmlhttp.readyState == 4)
		if (xmlhttp.status == 200)
		{
			window.location.reload(); 
		}
	    else if (request.status == 404)
	        alert("请求的链接不存在");
	    else
	        alert("错误: 错误代码 " + xmlhttp.status);
}

function deleteAdvertising(adId)
{
	getXmlHttpObject();
	var url = "../controllers/deleteAdvertisingController.php";
	var params = "adId=" + adId; 
	xmlhttp.open("POST",url,true);
	xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
	xmlhttp.onreadystatechange = deleteAdvertisingSuccess;
	xmlhttp.send(params);
}

function deleteAdvertisingSuccess()
{
	if (xmlhttp.readyState == 4)
		if (xmlhttp.status == 200)
		{

			window.location.reload(); 
		}
	    else if (request.status == 404)
	        alert("请求的链接不存在");
	    else
	        alert("错误: 错误代码 " + xmlhttp.status);
}

function jumpToUpdate(adId,title,summary,adUrl,category,status)
{
	var details = document.getElementById("details_"+adId).value;
	var temp = document.createElement("form");        
    temp.action = "../views/updatead.php";        
    temp.method = "post";        
    temp.style.display = "none";          
    var opt = document.createElement("textarea");        
    opt.name = "adId";        
    opt.value = adId;             
    temp.appendChild(opt); 
    
    
    var opt = document.createElement("textarea");        
    opt.name = "title";        
    opt.value = title;             
    temp.appendChild(opt);
    
    
    var opt = document.createElement("textarea");        
    opt.name = "summary";        
    opt.value = summary;             
    temp.appendChild(opt);
    
    
    var opt = document.createElement("textarea");        
    opt.name = "details";   
    opt.value = details;             
    temp.appendChild(opt);
    
    var opt = document.createElement("textarea");        
    opt.name = "adUrl";        
    opt.value = adUrl;             
    temp.appendChild(opt);
    
    
    var opt = document.createElement("textarea");        
    opt.name = "status";        
    opt.value = status;             
    temp.appendChild(opt);
    
    var opt = document.createElement("textarea");        
    opt.name = "category";        
    opt.value = category;             
    temp.appendChild(opt);
    document.body.appendChild(temp);        
    temp.submit();  
}

function updateAdvertising(adId)
{
	getXmlHttpObject();
	var title = document.getElementById("title").value;
	var summary = document.getElementById("summary").value;
	var adUrl = document.getElementById("adUrl").value;
	var details = document.getElementById("details").value;
	var status = document.getElementById("status").value;
	var category = document.getElementById("category").value;
	var url = "../controllers/updateAdvertisingController.php";
	var params = "adId=" + adId + "&title=" + title + "&summary=" + summary + "&details=" + details + "&adurl=" + adUrl + "&status=" + status + "&category=" + category; 
	xmlhttp.open("POST",url,true);
	xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
	xmlhttp.onreadystatechange = updateAdvertisingSuccess;
	xmlhttp.send(params);
}

function updateAdvertisingSuccess()
{	
	if (xmlhttp.readyState == 4)
	if (xmlhttp.status == 200)
	{
		window.location.href="../views/admanager.php"; 
	}
    else if (request.status == 404)
        alert("请求的链接不存在");
    else
        alert("错误: 错误代码 " + xmlhttp.status);
}
